package _16_设计;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class _211_添加与搜索单词数据结构设计 {

    public static void main(String[] args) {

        _211_添加与搜索单词数据结构设计 v = new _211_添加与搜索单词数据结构设计();

        v.addWord("at");
        v.addWord("dad");
        v.addWord("mad");
        System.out.println(v.search("pad"));
        System.out.println(v.search("bad"));
        System.out.println(v.search(".ad"));
        System.out.println(v.search("b.."));

    }

    private final HashMap<Integer, List<String>> containers;

    public _211_添加与搜索单词数据结构设计() {
        containers = new HashMap<>();
    }

    public void addWord(String word) {
        int len = word.length();
        List<String> list = containers.get(len);
        if (list == null) {
            list = new ArrayList<>();
            list.add(word);
            containers.put(len, list);
        } else {
            list.add(word);
        }
    }

    public boolean search(String word) {
        char[] chars1 = word.toCharArray();
        List<String> list = containers.get(chars1.length);
        if (list != null) {
            for (String str : list) {
                char[] chars2 = str.toCharArray();
                boolean isFind = true;
                // 对比每个字符是否相同
                for (int i = 0; i < chars1.length; i++) {
                    if (chars1[i] == '.' || chars2[i] == '.') continue;
                    if (chars1[i] != chars2[i]) {
                        isFind = false;
                        break;
                    }
                }
                if (isFind) return true;
            }
            return false;
        }
        return false;
    }


}
